<?php
class DirectoryController extends AppController {
	var $name = 'Directory';
	var $uses = array('Semester', "Assignment", 'Apartment', 'Term');
	var $helpers = array('Html', 'Form' );

	function index() {
		$this->set('semesters', $this->Semester->findAll());
	}

	function chooseApartment($semesterId = null) {
		if ($semesterId == null) {
			$this->redirect('directory');
			return;
		}

		$this->set('apartments', $this->Apartment->findAllBySemesterId($semesterId));
		$this->set('semesterId', $semesterId);
	}

	function listResidents($semesterId = null, $apartmentId = null) {
		if ($semesterId == null && $apartmentId == null) {
			$this->redirect('/directory');
			return;
		}

		if ($apartmentId == null) {
			$this->redirect("/directory/chooseApartment/$semesterId");
			return;
		}

		$users = $this->Assignment->findUsers($semesterId, $apartmentId);
		$this->set('users', $users);
		$this->Semester->recursive=0;
		$this->set('semester', $this->Semester->findBySemesterId($semesterId));
		$this->set('apartment', $this->Apartment->findByApartmentId($apartmentId));
	}

	function admin_index() {
		$this->index();
	}

	function admin_newSemester() {
		$this->set('termList', $this->Term->getTermList());
		$this->set('semesterList', $this->Semester->getSemesterList());
		if (empty($this->data)) {
			$this->render();
		} else {
			$this->cleanUpFields();
			$semesterId = $this->data['Semester']['semester_id'];
			echo "semesterId: $semesterId";
			return;
			if ($this->Semester->save($this->data)) {
				$this->Session->setFlash('The Semester has been saved');
				$this->redirect('/admin/semesters/');
			} else {
				$this->Session->setFlash('Please correct errors below.');
			}
		}
	}
}
?>
